package com.swinginwind.flexutils.components
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.containers.Panel;
	import mx.controls.Image;
	import mx.core.FlexGlobals;
	import mx.events.FlexMouseEvent;
	import mx.managers.PopUpManager;
	
	import spark.components.Label;
	import spark.effects.Fade;
	import mx.controls.Text;
	
	public class MsgBox extends Panel
	{
		private var _label:Text;
		private var _context:String;
		private var _image:Image;
		private var _imageType:int;
		
		private var _fadeEffect:FadeEffect;
		
		public static var imageType_warning:int = 1;
		public static var imageType_error:int = 2;
		public static var imageType_right:int = 3;
		
		[Bindable]
		[Embed(source="/com/swinginwind/flexutils/assets/16X16/information.png")]
		public var icoRight:Class
		
		[Bindable]
		[Embed(source="/com/swinginwind/flexutils/assets/16X16/error.png")]
		public var icoError:Class
		
		[Bindable]
		[Embed(source="/com/swinginwind/flexutils/assets/16X16/warning.png")]
		public var icoWarning:Class
		
		public function MsgBox(context:String,imageType:int)
		{
			super();
			this.setStyle("headerHeight",10);
			this.setStyle("borderRadius", 5);
			this.setStyle("backgroundColor", "black");
			this.setStyle("backgroundAlpha", 0.3);
			this._context = context;
			this._imageType = imageType;
			this._fadeEffect = new FadeEffect(this);
		}
		
		override protected function createChildren() : void {
			super.createChildren();
			addChild(createLabel());
			addChild(createImage());
		}
		
		public static function show(context:String,imageType:int = 1,parent:DisplayObject = null):void {
			var msgBox:MsgBox = new MsgBox(context,imageType);
			if(parent)
			{
				PopUpManager.addPopUp(msgBox, parent);
				msgBox.move(parent.width/2 - msgBox.width/2,10);
			}
			else
			{
				PopUpManager.addPopUp(msgBox,(DisplayObject)(FlexGlobals.topLevelApplication));
				PopUpManager.centerPopUp(msgBox);
			}
			
			msgBox._fadeEffect.fadeOut(3500);
			
			msgBox.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE,msgBox.removePopUp);
			msgBox.addEventListener(MouseEvent.MOUSE_OVER,msgBox.mouseOver);
			msgBox.addEventListener(MouseEvent.MOUSE_OUT,msgBox.mouseOut);
		}
		
		private function removePopUp(e:FlexMouseEvent):void {
			PopUpManager.removePopUp(this);	
		}
		
		private function mouseOver(e:MouseEvent):void {
			if (this._fadeEffect.isPlaying) {
				this._fadeEffect.fadeIn(0);
			}
		}
		
		private function mouseOut(e:MouseEvent):void {
			_fadeEffect.fadeOut(3500);
		}
		
		override protected function measure():void {
			//this._label.width = this._label.measureText(_context).width;
			//this._label.height = this._label.measureText(_context).
			this._label.width = this._label.measuredWidth;
			this._label.height = this._label.measuredHeight;
			measuredWidth = this._label.width + 32 + 48;
			measuredHeight = this._label.measuredHeight + 48;
		}
		
		override protected function updateDisplayList(param1:Number, param2:Number) : void {
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			this._label.move(36 + (this.width - this._label.width - 48)/2 - 10 ,(this.height - this._label.height)/2-10);
			//this._label.x = 28;
			//this._label.y = 12;
			this._image.x = 12;
			this._image.y = 12;
		}
		
		private function createImage():Image {
			_image = new Image();
			if (_imageType == MsgBox.imageType_error) {
				_image.source = icoError;
				_label.setStyle("color","red");
			} else if (_imageType == MsgBox.imageType_right) {
				_image.source = icoRight;	
			} else {
				_image.source = icoWarning;		
				_label.setStyle("color","red");
			}
			return _image;
		}
		
		private function createLabel():Text {
			_label = new Text();
			_label.setStyle("fontSize",13);
			_label.text = _context;
			_label.setStyle("backgroundAlpha", 0);
			return _label;
		}
	}
}
